/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kebut.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import kebut.main.MainKebut;

public class FileProcessor {
    
    public static byte[] plain;
    public static byte[] wav;
    public static byte[] plain_hasil;
    public static byte[] wav_hasil;
    
    public static String ext;
    
    public static File intxt, inwav, out;
    
    public static JFileChooser txDialog = new JFileChooser("Choose Text File");
    public static JFileChooser wavDialog = new JFileChooser("Choose WAV File");
    public static JFileChooser outDialog = new JFileChooser("Choose Directory for Output");

    public static void init() {
        wavDialog.setFileFilter(new FileFilter() {
            
            @Override
            public boolean accept(File f) {
                if (f.isDirectory()) {
                    return true;
                }               
                String s = f.getName(); 
                return s.endsWith(".wav")||s.endsWith(".WAV");
            }

            @Override
            public String getDescription() {
                return ("Audio Files(*wav,*WAV)");
            }        
        });
        
        outDialog.setApproveButtonText("Save");
    }
    
    public static void loadTx() throws IOException{
        int respon = txDialog.showOpenDialog(MainKebut.ui.frame);
        if (respon == JFileChooser.APPROVE_OPTION) {
            intxt = txDialog.getSelectedFile();
            MainKebut.ui.plainTx.setText(intxt.getAbsolutePath());
            
            //load file
            InputStream is = new FileInputStream(intxt);
            ext = intxt.getName();
            int dot = ext.indexOf(".");
            ext = ext.substring(dot+1, ext.length());
            long len = intxt.length();
            if (len > Integer.MAX_VALUE){
                JOptionPane.showMessageDialog(MainKebut.ui.frame, "Please choose smaller file");
            }else{
                plain = new byte[(int)len];
                is.read(plain, 0, plain.length);
                is.close();
            }
        }
    }
    
    public static void loadWav() throws IOException {
        int respon = wavDialog.showOpenDialog(MainKebut.ui.frame);
        if (respon == JFileChooser.APPROVE_OPTION) {
            try {
                inwav = wavDialog.getSelectedFile();
                
                //berhubungan dengan tampilan
                MainKebut.ui.wavTx.setText(inwav.getAbsolutePath());
                SoundPlayer.setMusic(inwav);
                
                InputStream is = new FileInputStream(inwav);
                if (inwav.getName().endsWith(".wav") || inwav.getName().endsWith(".WAV")){
                    long len = inwav.length();
                    if (len > Integer.MAX_VALUE){
                        JOptionPane.showMessageDialog(MainKebut.ui.frame, "Please choose smaller file");
                    }else{
                        wav = new byte[(int)len];
                        is.read(wav, 0, wav.length);
                        is.close(); 
                    }
                }               
            } catch (Exception ex) {
                throw new IOException ("Failed to read file.");
            }
        }        
    }
    
    public static void saveOutWav() {
        outDialog.setFileFilter(new FileFilter() {
            
            @Override
            public boolean accept(File f) {
                if (f.isDirectory()) {
                    return true;
                }               
                String s = f.getName();
                return s.endsWith(".wav")||s.endsWith(".WAV");
            }

            @Override
            public String getDescription() {
                return "Audio Files (*wav,*WAV)";
            }        
        });
        int respon = outDialog.showSaveDialog(MainKebut.ui.frame);
        if (respon == JFileChooser.APPROVE_OPTION) {
            try {
                String path = outDialog.getSelectedFile().getAbsolutePath();
                out = new File(path);
                    if (!(path.endsWith(".wav")||path.endsWith(".WAV"))){
                        if (path.indexOf(".") == -1){
                            path = path + ".wav";
                        }else{
                            path = path.substring(0, path.indexOf("."));
                            path = path + ".wav";
                        }
                    }
                    
                    MainKebut.ui.outTx.setText(path);
                    FileOutputStream wri = new FileOutputStream(path);
                    wri.write(wav_hasil);
                    wri.close();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(MainKebut.ui.frame, "Error saving file");
            }
        }
    }
    
    public static void saveOutTxt() {       
        outDialog.setFileFilter(new FileFilter() {
            
            @Override
            public boolean accept(File f) {
                if (f.isDirectory()) {
                    return true;
                }               
                String s = f.getName();
                return s.endsWith("." + ext)||s.endsWith("." + ext.toUpperCase());
            }

            @Override
            public String getDescription() {
                return "Any File";
            }        
        });
        int retVal = outDialog.showOpenDialog(MainKebut.ui.frame);
        
        if (retVal == JFileChooser.APPROVE_OPTION) {
            try {
                String path = outDialog.getSelectedFile().getAbsolutePath() + "." + ext;
                out = new File(path);
                FileOutputStream wri = new FileOutputStream(path);
                MainKebut.ui.outTx.setText(path);
                wri.write(plain_hasil);
                wri.close();
            } catch (IOException ioex) {
                JOptionPane.showMessageDialog(MainKebut.ui.frame, "Error saving file");
            }
        }
    }
}
